<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/26
 * Time: 14:33
 */

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\SgTrait;
use App\Models\OddsHandicap;
use App\Models\Def\MainGamePlat;

class sgqp extends Game
{
    use SgTrait;

    private $platName = 'sgqp';


    const API_PLAYER_REGISTER = '/Player/Register';
    const API_PLAYER_GET_BALANCE = '/Player/GetBalance';
    const API_PLAYER_TRANSACTION_IN = '/Transaction/PerformTransfer';
    const API_PLAYER_TRANSACTION_OUT = '/Transaction/PerformTransfer';
    const API_PLAYER_KICK = '/Player/TerminateSession';
    const API_PLAYER_CHECK_TRANSFER = '/Transaction/CheckTransferStatus';
    const API_PLAYER_JOIN_GAME = '/Game/NewLaunchGame';
    const API_PLAYER_JOIN_MOBILE_GAME = '/Game/NewLaunchMobileGame';
    const API_PLAYER_BET_RECORD = '/Report/GetBetLog';


    public function __construct()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'card',
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $accountUserName,
                'Currency' => 'CNY',
                'Password' => $password,
                'Country' => 'CN',
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_REGISTER, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $accountUserName,
                'ProductWallet' => $this->productWallet,
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_GET_BALANCE, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['balance' => $output['Balance']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $accountUserName,
                'ProductWallet' => $this->productWallet,
                'TransactionId' => $transferid,
                'Amount' => $price,
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_TRANSACTION_IN, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Status'] == 'Approved') {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        return $this->curlTransferIn($accountUserName, -$price, $transferid);
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $request->accountUserName,
                'ProductWallet' => $this->productWallet,
                'TransactionId' => $request->transferid,

            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_CHECK_TRANSFER, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            }elseif($output['Code'] == 541){
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败' . $output['Message']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '检查转账失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $accountUserName,
                'ProductWallet' => $this->productWallet,
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_KICK, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => [], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '删除玩家会话失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $request->accountUserName,
                'GameCode' => $request->gamecode,
                'ProductWallet' => $this->productWallet,
                'Language' => 'ZH-CN',
                'IpAddress' => $request->getClientIp(),
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_JOIN_GAME, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['url' => $output['GameUrl']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $request->accountUserName,
                'GameCode' => $request->gamecode,
                'ProductWallet' => $this->productWallet,
                'Language' => 'ZH-CN',
                'IpAddress' => $request->getClientIp(),
            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_JOIN_MOBILE_GAME, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['url' => $output['GameUrl']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $times = $this->parseTimeInterval($startTime, $endTime);
        foreach ($times as $time) {
            $start_time = $time['start'];
            $end_time = $time['end'];
            $totalPage = $page = 1;

            $json = [
                'json' => [
                    'MerchantCode' => $this->merchantCode,
                    'StartDate' => $start_time,
                    'EndDate' => $end_time,
                    'ProductWallet' => $this->productWallet,
                    'Page' => $page,
                    'PageSize' => '50000',
                    'Currency' => 'CNY',
                ]
            ];

            do {
                $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_BET_RECORD, [], [], $json);
                $output = json_decode($output, true);
                if (array_key_exists('Result', $output)) {
                    if ($output['Code'] == 0) {
                        $result = $output['Result'];
                        $totalPage = $output['Pagination']['TotalPage'];
                        if (count($result) >= 1) {
                            $data = array_merge($data, $result);
                        }
                    }
                }
                $page++;
            } while ($page <= $totalPage);
        }


        return $data;
    }
}